<template>
  <view>
    <!--选择收货地址-->
    <view class="address-choose-box" v-if="JSON.stringify(address)==='{}'">
      <button type="primary" size="mini" class="chooseAddressButton"@click="chooseAddress">請选择收货地址</button>
    </view>
    <!--收货地址信息-->
    <view class="address-information" v-else @click="chooseAddress">
      <view class="address-row1">
        <view class="left">
          <view class="consignee-name">收货人: {{address.userName}}</view>
        </view>
        <view class="right">
          <view class="consignee-phone">电話:{{address.telNumber}}</view>
          <uni-icons type="arrowright" size="16"></uni-icons>
        </view>
      </view>
      <view class="address-row2">
        <view class="left">收货地址: </view>
        <view class="right">{{addressStr}}</view>
      </view>
    </view>
    
    <!--分割線-->
    <image src ="https://7072-prod-2gdjc66eb3276dce-1311299937.tcb.qcloud.la/static/farm_shop_mall/swiperImage/splitBar.png" class="bottom-split-line"></image>
  </view>
</template>

<script>
  import {mapState, mapMutations,mapGetters}from 'vuex'
  export default {
    name:"address-bar",
    data() {
      return {
        
      };
    },
    methods:{
      //更新地址到vuex
      ...mapMutations('user',['updateAddress']),
      async chooseAddress(){
        const [err, succ] = await uni.chooseAddress().catch(err => err)
        
        if(err === null && succ.errMsg === 'chooseAddress:ok'){
          this.updateAddress(succ)
        }
        //若沒有地址权限，則要重新拿
        if(err && (err.errMsg === 'chooseAddress:fail auth deny'||err.errMsg === 'chooseAddress:fail authorize no response')){
          this.reAuth()
        }
      },
      async reAuth(){
        const [err2, confirmResult] = await uni.showModal({
          content:'打开地址权限啊!',
          confirmText: '確認',
          cancelText:'取消'
        })
        if(err2)return
        if(confirmResult.cancel) return uni.$showMessage('取消干嘛!')
        if(confirmResult.confirm) return uni.openSetting({
          success:(settingResult) => {
            if(settingResult.authSetting['scope.address']) return uni.$showMessage('取消干嘛!')
            if(settingResult.authSetting['scope.address']) return uni.$showMessage('成功了!')
          }
        })
      }
    },
    computed:{
      //从vuex拿地址
      ...mapState('user', ['address']),
      ...mapGetters('user',['addressStr'])

    }
  }
</script>

<style lang="scss">
.bottom-split-line{
  display: block;
  width: 100%;
  height: 10px;
}
.address-choose-box{
  height: 90px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.address-information{
  font-size: 12px;
  height: 90px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding:0 5px;
  .address-row1{
    display: flex;
    justify-content: space-between;
    .left{
      .consignee-name{
        
      }
    }
    .right{
      display: flex;
      justify-content: space-between;
      .phone{
        
      }
      
    }
  }
  .address-row2{
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 10px;
    .left{
      white-space: nowrap;
      
    }
    .right{
      
    }
    
  }
}
</style>
